package com.pearadmin.pro.modules.experiment.controller;

import com.pearadmin.pro.common.aop.annotation.Log;
import com.pearadmin.pro.common.aop.annotation.Repeat;
import com.pearadmin.pro.common.constant.ControllerConstant;
import com.pearadmin.pro.common.context.UserContext;
import com.pearadmin.pro.common.web.base.BaseController;
import com.pearadmin.pro.common.web.base.page.PageRequest;
import com.pearadmin.pro.common.web.base.page.PageResponse;
import com.pearadmin.pro.common.web.domain.Result;
import com.pearadmin.pro.modules.experiment.entity.*;
import com.pearadmin.pro.modules.experiment.service.ExperimentAccessoryService;
import com.pearadmin.pro.modules.experiment.service.ExperimentBulletinService;
import com.pearadmin.pro.modules.experiment.service.ExperimentService;
import com.pearadmin.pro.modules.sys.domain.SysUser;
import com.pearadmin.pro.modules.sys.service.SysUserService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.*;

/**
 * 实验空间新闻公告
 *
 * @author ZhouXingYun
 * @date 2022/6/14 16:50
 */
@Api(tags = {"实验空间新闻公告"})
@RestController
@RequestMapping(ControllerConstant.PREFIX_SYS + "ebc")
public class ExperimentBulletinController extends BaseController {

    @Resource
    private ExperimentBulletinService experimentBulletinService;

    @Resource
    private SysUserService sysUserService;

    @Resource
    private ExperimentAccessoryService experimentAccessoryService;

    @Resource
    private UserContext userContext;

    /**
     * 获取公告列表
     *
     * @return {@link Result}
     */
    @GetMapping("getAnnouncementList")
    @Log(title = "获取公告列表")
    @ApiOperation(value = "获取公告列表")
    public Result getAnnouncementList( PageRequest request) {
        PageResponse<ExperimentBulletin> bulletinList = experimentBulletinService.getAnnouncementList(request);
        List<ExperimentBulletin> record = bulletinList.getRecord();
        record.forEach(experimentBulletin -> {
            String ebCreateuser = experimentBulletin.getEbCreateuser();
            SysUser byId = sysUserService.getById(ebCreateuser);
            if(byId!=null){
                experimentBulletin.setEbCreateUserName(byId.getNickname());
            }
        });
        return success(bulletinList);
    }
    /**
     * 获取新闻列表
     *
     * @return {@link Result}
     */
    @GetMapping("getNewsList")
    @Log(title = "获取新闻列表")
    @ApiOperation(value = "获取新闻列表")
    public Result getNewsList( PageRequest request) {
        PageResponse<ExperimentBulletin> bulletinList = experimentBulletinService.getNewsList(request);
        List<ExperimentBulletin> record = bulletinList.getRecord();
        record.forEach(experimentBulletin -> {
            String ebCreateuser = experimentBulletin.getEbCreateuser();
            SysUser byId = sysUserService.getById(ebCreateuser);
            if(byId!=null){
                experimentBulletin.setEbCreateUserName(byId.getNickname());
            }
        });
        return success(bulletinList);
    }

    /**
     * 查看个人详情
     *
     * @return {@link Result}
     */
    @GetMapping("viewDetails")
    @Log(title = "查看新闻/公告详情")
    @ApiOperation(value = "查看新闻/公告详情")
    public Result viewDetails(@RequestParam String id) {
        ExperimentBulletin bulletinList = experimentBulletinService.viewDetails(id);
        return success(bulletinList);
    }



    /**
     * 新增新闻
     * @author ZhouXingYun
     * @date 2022/6/22 14:12
     * @param experimentBulletin
     * @return com.pearadmin.pro.common.web.domain.Result
     */
    @Repeat
    @PostMapping("save")
    @Log(title = "新增新闻")
    @ApiOperation(value = "新增新闻")
    public Result save(@RequestBody  ExperimentBulletin experimentBulletin) {
        experimentBulletin.setEbCreateuser(userContext.getUserId());
        experimentBulletin.setEbCreatetime(new Date());
        boolean save = experimentBulletinService.insertSelective(experimentBulletin);
        if(experimentBulletin.getAccessoryList().size()>0){
            for (int i = 0; i < experimentBulletin.getAccessoryList().size(); i++) {
                String adress = experimentBulletin.getAccessoryList().get(i);
                ExperimentAccessory experimentAccessory = new ExperimentAccessory();
                experimentAccessory.setEaAdress(adress);
                experimentAccessory.setEbId(experimentBulletin.getId());
                experimentAccessory.setEaName("附件"+(i+1));
                experimentAccessoryService.save(experimentAccessory);
            }
        }
        return auto(save);
    }

    /**
     * 修改新闻
     * @author ZhouXingYun
     * @date 2022/6/22 9:09
     * @param experimentBulletin
     * @return com.pearadmin.pro.common.web.domain.Result
     */
    @PutMapping("edit")
    @Log(title = "修改新闻")
    @ApiOperation(value = "修改新闻")
    public Result edit(@RequestBody ExperimentBulletin experimentBulletin) {
        experimentBulletin.setEbUpdatetime(new Date());
        experimentBulletin.setEbUpdateuser(userContext.getUserId());
        return auto(experimentBulletinService.updateById(experimentBulletin));
    }

    /**
     * 删除新闻
     * @author ZhouXingYun
     * @date 2022/6/22 10:21
     * @param id
     * @return com.pearadmin.pro.common.web.domain.Result
     */
    @DeleteMapping("remove")
    @Log(title = "删除新闻")
    @ApiOperation(value = "删除新闻")
    public Result remove(@RequestParam String id){
        return auto(experimentBulletinService.removeById(id));
    }

    /**
     * 批量删除新闻
     * @author ZhouXingYun
     * @date 2022/6/22 10:21
     * @param ids
     * @return com.pearadmin.pro.common.web.domain.Result
     */
    @DeleteMapping("removeBatch")
    @Log(title = "批量删除新闻")
    @ApiOperation(value = "批量删除新闻")
    public Result removeBatch(@RequestParam List<String> ids) { return auto(experimentBulletinService.removeByIds(ids)); }



}
